#
# @lc app=leetcode.cn id=874 lang=python3
#
# [874] 模拟行走机器人
#

# @lc code=start
from typing import List
class Solution:
    def robotSim(self, commands: List[int], obstacles: List[List[int]]) -> int:
        obstaclesSet = set([])
        for obs in obstacles:
            obstaclesSet.add((obs[0], obs[1]))
        
        # 北， 东， 南，西 4个方向上每一移动一个单位，对应的x,y值的变化
        dx = [0, 1, 0, -1]
        dy = [1, 0, -1, 0]
        
        direction = 0
        currentX = 0
        currentY = 0
        
        result = 0
        
        for com in commands:
            if com == -2:
                direction = (direction - 1) % 4
            elif com == -1: 
                direction = (direction + 1) % 4
            elif com >= 1 and com <= 9:
                while com > 0:
                    nextX, nextY = currentX + dx[direction], currentY + dy[direction]
                    if (nextX, nextY) not in obstaclesSet:
                        currentX, currentY = nextX, nextY
                        result = max(result, currentX*currentX + currentY*currentY)
                    else:
                        break
                    
                    com -= 1
        return result
        
        
# @lc code=end

